# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: SpepS <dreamspepser at yahoo dot it>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: DonVla <donvla@users.sourceforge.net>

pkgname=zynaddsubfx
pkgver=3.0.6
pkgrel=4
_mruby_zest_commit='dd3d45ba333011f2359e86f9a758d9836e53fe5f'  # refs/tags/3.0.6
pkgdesc="Opensource software synthesizer capable of making a countless number of instruments."
arch=(loong64 x86_64)
url="http://zynaddsubfx.sourceforge.net"
license=(GPL2)
groups=(lv2-plugins pro-audio vst-plugins)
# NOTE: fftw is dlopen()'ed
depends=(fftw gcc-libs glibc hicolor-icon-theme libglvnd libuv ttf-roboto zlib)
makedepends=(alsa-lib asciidoc asciidoctor cmake cxxtest doxygen git jack liblo
libxpm mxml portaudio rtosc ruby-rake)
optdepends=(
  'lv2-host: for using the LV2 plugin'
  'vst-host: for using the VST plugin'
)
source=(
  https://downloads.sourceforge.net/project/$pkgname/$pkgname/$pkgver/$pkgname-$pkgver.tar.bz2
  $pkgname-3.0.6-system_rtosc.patch
  $pkgname-3.0.6-libzest_location.patch
  $pkgname-mruby-zest-build::git+https://github.com/mruby-zest/mruby-zest-build#commit=$_mruby_zest_commit
  $pkgname-mruby::git+https://github.com/mruby/mruby
  $pkgname-nanovg::git+https://github.com/memononen/nanovg
  $pkgname-mruby-sleep::git+https://github.com/matsumoto-r/mruby-sleep
  $pkgname-mruby-dir-glob::git+https://github.com/gromnitsky/mruby-dir-glob
  $pkgname-mruby-regexp-pcre::git+https://github.com/iij/mruby-regexp-pcre
  $pkgname-mruby-set::git+https://github.com/yui-knk/mruby-set
  $pkgname-mruby-io::git+https://github.com/iij/mruby-io
  $pkgname-mruby-glew::git+https://github.com/IceDragon200/mruby-glew
  $pkgname-mruby-glfw3::git+https://github.com/IceDragon200/mruby-glfw3
  $pkgname-mruby-nanovg::git+https://github.com/mruby-zest/mruby-nanovg
  $pkgname-mruby-qml-spawn::git+https://github.com/mruby-zest/mruby-qml-spawn
  $pkgname-mruby-qml-parse::git+https://github.com/mruby-zest/mruby-qml-parse
  $pkgname-mruby-complex::git+https://github.com/pbosetti/mruby-complex
  $pkgname-mruby-dir::git+https://github.com/iij/mruby-dir
  $pkgname-mruby-errno::git+https://github.com/iij/mruby-errno
  $pkgname-mruby-file-stat::git+https://github.com/ksss/mruby-file-stat
  $pkgname-mruby-process::git+https://github.com/iij/mruby-process
  $pkgname-mruby-zest-build-3.0.6-devendor_rtosc.patch
  $pkgname-mruby-zest-build-3.0.6-libzest_location.patch
  $pkgname-mruby-zest-build-3.0.6-system_wide_location.patch
  $pkgname-mruby-zest-build-3.0.6-cflags_ldflags.patch
  $pkgname-mruby-zest-build-3.0.6-makefile_find.patch
)
sha512sums=('86311f00d97e20c7eb22a46c99736c56765ed9737e728ef380a324e4a92c731086ed74f5d7bfeae104b690ab1ee1b40bfb8f240bc4c46f8afd47308ec48cbcea'
            'c84d185e7bcc3d7b0bd44941999b9b14638e230c5c5d86b487a39d6b793b56649b6aacde8d27b0aef567eaa3d381d3906d7bd31fd5fd74d5a662e21d188fcdc9'
            '695f5776b07e4ace611f358ea1007e0023f1ba6dcf4ebb85974b461490850c51bb85903abafc07b23236051ab161cc0e538a127a11efd0b1dfd129dc99f37d77'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '0677bf66e5996b09298a3f7e3aabf0d5d8a5bbdca6ed869038e1a35cbef94c2bafcfeb92cd163f43476aadf5c915cde92c75876c5ff3964d5b2049509075a765'
            'd7ef0b4e8068d1d09d34c913d7b70342eaaa2eb8dae619d223e3e03ac667cf76c6e5615b60efabaf7eae594e3a5d6f40bc0cf10b8e939da1aa349bc27dff4ea8'
            '2a6b57df3a9ffac669341d37739b281ce32696f82d82ee17220fdc9e426729d40f91cf50e1b58b7be1cc9bc5651d99794ae5e1bbd32f25c14aaf489989e10aab'
            'a3912a879cb78565934a5cdea97ac1623f65a02d26ded548d851cd271da62390eb3fa9f26dfff153eb2d18c5bf30ba47e19fcbf1985e4b03772d2df936998511'
            '85174aeb46883b330bdeccb3519c30e14270cd1f6088970869af55c7d9edce3db0a6132fd4dec23120af5386463e31ee3d8abb093f9f4c81607deafbc6d2fae2')
b2sums=('4adbeffb9bc0bd77bf81a6734f5c26fc676426e644e27cebb8e54e8d74d869c0e1b468782cd1a64aacef8f9b788d434a8d30a194944650eea03c4fd146ee7bf7'
        'd9586ef1dd7866963d342f9be8def639ab4428a173f8abc2e503643d87e2c9087ff8d6af538df7c8f8df8f8facd409e8665735ef905deae8dfe3a840de340ea1'
        '68a24898a4646c6d84e782c4e31a962de456fa315d5c398c16b013efa684683f80cc411bf51628bb9cff799fc77f669d56e5d44af5938e05d0ec64322ad30ec7'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        '70ea96032e5f3f20f4270339f03b508c5a5e5d62859d6771adce452fbcde48f89edb4ef8908255cbf915f1088ce5d3fc5baa1b1e74794db3bc975e48c3425d14'
        '2f1782fd907fb0e284c16ee0f09e1d93d05984676707da2d9d47038880e723379535f8d3934f97753cc9a6865aed957a1c230840dda3a2cb49a26cb4ff2cd034'
        '69ccb494e863caf8e47eaa70609940d532cfae6265eeffc626f24ed6a2290511d828d63405a7560fd567b0ab01bdabedd85aa3fe28bec5a638477654f9440f40'
        'cab9048aede5bd346493ede996752b902f78de95de97310622caa0f7e473eba46159f1ee5fc925cf9ffc3cf1bd3f98c6dac521f354ac1be074346015bf9ded14'
        '1d90dd20faf7b2b4b50417b8f614779237c29c2f721c1e0b9c3a7546f9cf991983e05821d204d2d39931d42083fb348683306e8107438a1ed94d2fa34d90606b')

prepare() {
  # https://github.com/zynaddsubfx/zynaddsubfx/pull/170
  # https://github.com/zynaddsubfx/zynaddsubfx/pull/171
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-3.0.6-system_rtosc.patch
  # load libzest.so from custom system location in /usr/lib/zynaddsubfx/
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-3.0.6-libzest_location.patch
  # disable broken PortChecker test: https://github.com/zynaddsubfx/zynaddsubfx/issues/173
  sed -e '/add_test(PortChecker/d' -i $pkgname-$pkgver/src/Tests/CMakeLists.txt

  (
    cd $pkgname-mruby-zest-build
    git submodule init
    git submodule deinit deps/rtosc
    git submodule deinit deps/libuv
    git config submodule.mruby.url "$srcdir/$pkgname-mruby"
    git config submodule.deps/nanovg.url "$srcdir/$pkgname-nanovg"
    git config submodule.deps/mruby-sleep.url "$srcdir/$pkgname-mruby-sleep"
    git config submodule.deps/mruby-dir-glob.url "$srcdir/$pkgname-mruby-dir-glob"
    git config submodule.deps/mruby-regexp-pcre.url "$srcdir/$pkgname-mruby-regexp-pcre"
    git config submodule.deps/mruby-set.url "$srcdir/$pkgname-mruby-set"
    git config submodule.deps/mruby-io.url "$srcdir/$pkgname-mruby-io"
    git config submodule.deps/mruby-glew.url "$srcdir/$pkgname-mruby-glew"
    git config submodule.deps/mruby-glfw3.url "$srcdir/$pkgname-mruby-glfw3"
    git config submodule.deps/mruby-nanovg.url "$srcdir/$pkgname-mruby-nanovg"
    git config submodule.deps/mruby-qml-spawn.url "$srcdir/$pkgname-mruby-qml-spawn"
    git config submodule.deps/mruby-qml-parse.url "$srcdir/$pkgname-mruby-qml-parse"
    git config submodule.deps/mruby-complex.url "$srcdir/$pkgname-mruby-complex"
    git config submodule.deps/mruby-dir.url "$srcdir/$pkgname-mruby-dir"
    git config submodule.deps/mruby-errno.url "$srcdir/$pkgname-mruby-errno"
    git config submodule.deps/mruby-file-stat.url "$srcdir/$pkgname-mruby-file-stat"
    git config submodule.deps/mruby-process.url "$srcdir/$pkgname-mruby-process"
    git -c protocol.file.allow=always submodule update
    # use system provided rtosc
    patch -Np1 -i ../$pkgname-mruby-zest-build-3.0.6-devendor_rtosc.patch
    # load libzest.so from custom system location in /usr/lib/zynaddsubfx/
    patch -Np1 -i ../$pkgname-mruby-zest-build-3.0.6-libzest_location.patch
    # load qml data from custom system location in /usr/share/zynaddsubfx/qml/
    # load JSON schema from custom system location in /usr/share/zynaddsubfx/schema/
    # load fonts from custom system location in /usr/share/fonts/TTF/
    patch -Np1 -i ../$pkgname-mruby-zest-build-3.0.6-system_wide_location.patch
    # incorporate LDFLAGS to achieve full RELRO: https://github.com/mruby-zest/mruby-zest-build/pull/80
    patch -Np1 -i ../$pkgname-mruby-zest-build-3.0.6-cflags_ldflags.patch
    # use find internals: https://github.com/mruby-zest/mruby-zest-build/pull/81
    patch -Np1 -i ../$pkgname-mruby-zest-build-3.0.6-makefile_find.patch
  )
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D DefaultInput=jack
    -D DefaultOutput=jack
    -D GuiModule=zest
    -D ZYN_SYSTEM_RTOSC=ON
    -S $pkgname-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
  make man -C $pkgname-$pkgver/doc
  # TODO: fix full RELRO
  make -C $pkgname-mruby-zest-build
}

check() {
  make -k test ARGS='-VVd' -C build
}

package() {
  depends+=(
    alsa-lib libasound.so
    jack libjack.so
    liblo liblo.so
    mxml libmxml.so
    portaudio libportaudio.so
    rtosc librtosc.so librtosc-cpp.so
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname-$pkgver/$pkgname.svg -t "$pkgdir/usr/share/icons/hicolor/scalable/apps/"
  install -vDm 644 $pkgname-$pkgver/doc/$pkgname.1 -t "$pkgdir/usr/share/man/man1/"

  cd $pkgname-mruby-zest-build
  install -vDm 755 zest "$pkgdir/usr/bin/zyn-fusion"
  install -vDm 755 libzest.so -t "$pkgdir/usr/lib/$pkgname/"
  install -vDm 644 src/mruby-zest/{example,qml}/*.qml -t "$pkgdir/usr/share/$pkgname/qml/"
  install -vDm 644 src/osc-bridge/schema/test.json -t "$pkgdir/usr/share/$pkgname/schema/"
  install -vDm 644 completions/zyn-fusion -t "$pkgdir/usr/share/bash-completion/completions/"
}
